
from rest_framework import serializers
from user.models import *

# 流程序列化器
class FlowSer(serializers.ModelSerializer):

    sections = serializers.PrimaryKeyRelatedField(queryset=SectionModel.objects.all())

    sections_name = serializers.SerializerMethodField()

    def get_sections_name(self, obj):
        return obj.sections.name

    class Meta:
        model = FlowModel
        fields = '__all__'

# 流程节点序列化器
class FlowNodeSer(serializers.ModelSerializer):
    stations = serializers.PrimaryKeyRelatedField(queryset=StationModel.objects.all())
    flows = serializers.PrimaryKeyRelatedField(queryset=FlowModel.objects.all())

    stations_name = serializers.SerializerMethodField()
    flows_name = serializers.SerializerMethodField()

    def get_stations_name(self, obj):
        return obj.stations.name
    def get_flows_name(self, obj):
        return obj.flows.name

    class Meta:
        model = FlowNodeModel
        fields = '__all__'

# 节点内容类型序列化器
class NodeTypeSer(serializers.ModelSerializer):
    nodes = serializers.PrimaryKeyRelatedField(queryset=FlowNodeModel.objects.all())

    nodes_name = serializers.SerializerMethodField()
    def get_nodes_name(self, obj):
        return obj.nodes.name

    class Meta:
        model = NodeTypeModel
        fields = '__all__'

# 节点内容序列化器
class FlowContentSer(serializers.ModelSerializer):
    type = serializers.PrimaryKeyRelatedField(queryset=NodeTypeModel.objects.all())

    type_title = serializers.SerializerMethodField()
    def get_type_title(self, obj):
        return obj.type.title

    class Meta:
        model = FlowContentModel
        fields = '__all__'

# 审批流程序列化器
class HistorySer(serializers.ModelSerializer):
    is_status = serializers.ChoiceField(choices=HistoryModel.STATUS, source="get_is_status_display", read_only=True)

    users = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    flows = serializers.PrimaryKeyRelatedField(queryset=FlowModel.objects.all())


    users_name = serializers.SerializerMethodField()
    flows_name = serializers.SerializerMethodField()

    def get_users_name(self, obj):
        return obj.users.name

    def get_flows_name(self, obj):
        return obj.flows.name

    class Meta:
        model = HistoryModel
        fields = '__all__'

# 节点审批序列化器
class ApprovalSer(serializers.ModelSerializer):
    is_status = serializers.ChoiceField(choices=ApprovalModel.STATUS, source="get_is_status_display", read_only=True)

    # historys = serializers.PrimaryKeyRelatedField(queryset=HistoryModel.objects.all())
    nodes =  serializers.PrimaryKeyRelatedField(queryset=FlowNodeModel.objects.all())
    stations = serializers.PrimaryKeyRelatedField(queryset=StationModel.objects.all())

    # historys_name = serializers.SerializerMethodField()
    nodes_name = serializers.SerializerMethodField()
    stations_name = serializers.SerializerMethodField()

    # def get_historys_name(self, obj):
    #     return obj.historys.name
    def get_nodes_name(self, obj):
        return obj.nodes.name
    def get_stations_name(self, obj):
        return obj.stations.name

    class Meta:
        model = ApprovalModel
        fields = '__all__'